<?php
/**
 * 门禁服务
 */
namespace service;

use think\Cache;
use think\Request;
use think\Db;

class GuardService
{
    private $api_gd_url = "http://192.168.8.110:8098/api";
    private $api_token = "6DB1FDAE59DC2D9C6F66CB5A7461A16897389E7C099A75B959801A0BE6BF5AF3";

    private $count = 1;

    protected $user_qrcode_key = "qrcode:";

    /**
     * @var object 对象实例
     */
    protected static $instance;

    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return \think\Request
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    //2.13.1 增加/编辑人员信息
    //请求路径 http://serverIP:serverPort/api/person/add?access_token={apitoken}
    //请求方式 POST
    public function AddPerson($user){
        if(empty($user)){
            return ['code'=>-1,'message'=>'参数不能为空！','data'=>null];
        }
        $url = $this->api_gd_url."/person/add?access_token={$this->api_token}";
        if(count(explode(",",$user['department_ids'])) > 1){
            $user['department_ids'] = Db::name("department")->where(['status'=>1])->where(['is_jtzb'=>1])->where(['department_id'=>['in',$user['department_ids']]])->order("id asc")->value("department_id");
        }
        $data = [
            "pin" => $user['user_id'],
            "deptCode" => $user['department_ids'],
            "isDisabled" => false,
            "isSendMail" => false,
            "mobilePhone" => $user['mobile'],
            "name" => $user['username'],
            "gender" => $user['gender'] == 1 ? "M" : "F",
        ];
        $res = RequestService::post_data($url, $data);
        //dump($res);exit;
        echo $this->count++."---".$user['department_ids']."---".$user['user_id']."---".$res['message'].PHP_EOL;
        return $res;
    }

    //2.13.7 增加/编辑人员基础信息
    //请求路径 http://serverIP:serverPort/api/person/addPersonnelBasicInfo?access_token={apitoken}
    //请求方式 POST
    public function AddPersonnelBasicInfo($user){
        if(empty($user)){
            return ['code'=>-1,'message'=>'参数不能为空！','data'=>null];
        }
        $url = $this->api_gd_url."/person/addPersonnelBasicInfo?access_token={$this->api_token}";
        if(count(explode(",",$user['department_ids'])) > 1){
            $user['department_ids'] = Db::name("department")->where(['status'=>1])->where(['is_jtzb'=>1])->where(['department_id'=>['in',$user['department_ids']]])->order("id asc")->value("department_id");
        }
        $data = [
            "pin" => $user['user_id'],
            "deptCode" => $user['department_ids'],
            "isDisabled" => false,
            "isSendMail" => false,
            "mobilePhone" => $user['mobile'],
            "name" => $user['username'],
            "gender" => $user['gender'] == 1 ? "M" : "F",
        ];
        $res = RequestService::post_data($url, $data);
        //dump($res);exit;
        echo $this->count++."---".$user['department_ids']."---".$user['user_id']."---".$res['message'].PHP_EOL;
        return $res;
    }

    //2.13.2 通过 pin 删除人员
    //请求路径 http://serverIP:serverPort/api/person/delete/{pin}?access_token={apitoken}
    //请求方式 POST
    public function DelPerson($user_id){
        if(empty($user_id)){
            return ['code'=>-1,'message'=>'参数不能为空！','data'=>null];
        }
        $url = $this->api_gd_url."/person/delete/{$user_id}?access_token={$this->api_token}";
        $data = [];
        dump([$url,$data]);
        $res = RequestService::post_data($url, $data);
        dump($res);exit;
        return $res;
    }

    //2.5.1 新增/编辑部门
    //请求路径 http://serverIP:serverPort/api/department/add?access_token={apitoken}
    //请求方式 POST
    public function AddDepartment($department=[]){
        if(empty($department)){
            return ['code'=>-1,'message'=>'参数不能为空！','data'=>null];
        }
        $url = $this->api_gd_url."/department/add?access_token={$this->api_token}";
        $data = [
            "name" => str_replace(['（','）','、',' '],"",$department['title']).$department['pid'],
            "code" => $department['department_id'],
            "parentCode" => $department['pid'],
            "sortNo" => $department['sort'],
        ];
        //dump([$url,$data]);
        $res = RequestService::post_data($url, $data);
        //dump($res);exit;
        echo $this->count++."---".$department['department_id']."---".$department['title']."---".$res['message'].PHP_EOL;
        return $res;
    }

    //2.5.2 根据部门编号删除部门
    //请求路径 http://serverIP:serverPort/api/department/delete/{code}?access_token={apitoken}
    //请求方式 POST
    public function DelDepartment($department_id){
        if(empty($department_id)){
            return ['code'=>-1,'message'=>'参数不能为空！','data'=>null];
        }
        $url = $this->api_gd_url."/department/delete/{$department_id}?access_token={$this->api_token}";
        $data = [];
        //dump([$url,$data]);
        $res = RequestService::post_data($url, $data);
        //dump($res);exit;
        return $res;
    }

    //2.13.5 根据 pin 获取动态二维码
    //请求路径 http://serverIP:serverPort/api/person/getQrCode/{pin}?access_token={apitoken}
    //请求方式 POST
    public function getqrcode($user_id){
        $user_id = 10010;
        $ret = '';
        if(empty($user_id)){
            return $ret;
        }
        $this->user_qrcode_key = $this->user_qrcode_key.$user_id;
        $ret = Cache::store('redis')->get($this->user_qrcode_key);
        if(empty($ret)){
            $url = $this->api_gd_url."/person/getQrCode/{$user_id}?access_token={$this->api_token}";
            $data = [];
            //dump([$url,$data]);
            $res = RequestService::post_data($url, $data);
            //dump($res);exit;
            if($res['code'] == 0 && $res['message'] == 'success'){
                $ret = $res['data'];
                Cache::store('redis')->set($this->user_qrcode_key, $res['data'], 280);
            }
        }
        return $ret;
    }

    //2.1.4 获取权限组集合
    //请求路径 http://serverIP:serverPort/api/accLevel/list?pageNo={pageNo}&pageSize={pageSize}&access_token={apitoken}
    //请求方式 GET
    public function Getacclevellists(){
        $url = $this->api_gd_url."/accLevel/list?pageNo=1&pageSize=100&access_token={$this->api_token}";
        $data = [];
        $res = RequestService::curl_request($url,$data,"GET");
        //dump($res);
        return json_decode($res['content'],true);
    }

}